<template>
  <div>
    <FormPanel ref="formPanel" :list="formList" :formType="type" @formChange="formChangeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="110" />
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <el-button @click="closeDialog">关 闭</el-button>
      <multiuse-button type="primary" :click="saveHandler">保 存</multiuse-button>
    </div>
    <!-- 经销商查询 -->
    <BaseDialog :visible.sync="isDealer" title="经销商查询" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <OwnerSearch :rowData="formData" @close="emitCloseHandler1" />
    </BaseDialog>

    <BaseDialog :visible.sync="infoVisible" :title="'选装信息'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <BaseInfo :rowData="formData" @close="emitCloseHandler" />
    </BaseDialog>
  </div>
</template>
<script>
import moment from 'moment';
import { dictionary } from '@/utils/dictMixin';
import { findById, saveRule } from '@/api/omSaleP/vehicleParameters/RebateManage/RebateRulesSet';
import BaseInfo from './BaseInfo';
import OwnerSearch from './OwnerSearch';

export default {
  mixins: [dictionary],
  props: ['type', 'rowData'],

  data() {
    this.saveData = null;
    return {
      formList: this.createdFrom(),
      formData: {},
      infoVisible: false,
      isDealer: false,
      handleData: []
    };
  },
  components: {
    BaseInfo,
    OwnerSearch
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      if (this.type == 'edit') {
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      } else {
        // this.rowData = {};
      }
    },
    optionCodeSearch() {
      this.infoVisible = true;
    },
    dealerCodeSearch() {
      this.isDealer = true;
    },
    formChangeHandle(val) {
      
      this.saveData = val;
    },
     emitCloseHandler(val, data) {

      this.infoVisible = val;
      if (data) {
        this.handleData = data;
        //this.formList.find(x => x.fieldName == 'modelName').initialValue = data[0].modelName;
        this.formList.find(x => x.fieldName == 'optionCode').initialValue = data[0].optionCode;
        this.formList.find(x => x.fieldName == 'optionName').initialValue = data[0].optionName;
        this.formList.find(x => x.fieldName == 'packageName').initialValue = data[0].packageName;
        this.formList.find(x => x.fieldName == 'modelName').initialValue = data[0].modelName;
        // this.formList.find(x => x.fieldName == 'powerType').initialValue = parseInt(data[0].powerType);
      }
    },
    async saveHandler() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        let saveData = Object.assign({}, this.rowData, this.saveData);
 
        if (saveData.optionCode) {
          saveData.modelId = this.handleData.length > 0 ? this.handleData[0].modelId : this.rowData.modelId;
          
          saveData.modelCode = this.handleData.length > 0 ? this.handleData[0].modelCode : this.rowData.modelCode;
          saveData.optionId = this.handleData.length > 0 ? this.handleData[0].optionId : this.rowData.optionId;
          saveData.optionCode = this.handleData.length > 0 ? this.handleData[0].optionCode : this.rowData.optionCode;

          saveData.brandName = this.handleData.length > 0 ? this.handleData[0].brandName : this.rowData.brandName;
          saveData.seriesName = this.handleData.length > 0 ? this.handleData[0].seriesName : this.rowData.seriesName;
          saveData.packageName = this.handleData.length > 0 ? this.handleData[0].packageName : this.rowData.packageName;

          saveData.modelName = this.handleData.length > 0 ? this.handleData[0].modelName : this.rowData.modelName;
          saveData.optionName = this.handleData.length > 0 ? this.handleData[0].optionName : this.rowData.optionName;
          saveData.configName = this.handleData.length > 0 ? this.handleData[0].configName : this.rowData.configName;
          
        } else {
          saveData.modelId = '';
          saveData.modelCode = '';
          saveData.optionId = '';
          saveData.optionCode = '';
          saveData.brandName = '';
          saveData.seriesName = '';
          saveData.packageName = '';
          saveData.modelName='';
          saveData.optionName='';
          saveData.configName='';
        }
        let res = {};
        if (this.type === 'add') {
          res = await saveRule(saveData);
        }
        if (this.type === 'edit') {
          res = await saveRule(saveData);
        }
        if (res.resultCode === 200) {
          await this.closeDialog('refresh');
        }
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    },
   
    emitCloseHandler1(val, data) {
      this.isDealer = val;
      if (data) {
        this.formList.find(x => x.fieldName == 'dealerCode').initialValue = data[0].dealerCode;
        this.formList.find(x => x.fieldName == 'dealerName').initialValue = data[0].dealerName;
      }
    },
    createdFrom() {
      return [
        // {
        //   type: 'INPUT',
        //   label: '选装代码',
        //   fieldName: 'optionCode',
        //   placeholder: '请选择',
        //   initialValue: '',
        //   unitRender: () => {
        //     return <el-button icon="el-icon-search" onClick={() => this.optionCodeSearch()}></el-button>;
        //   },
        //   change: val => {
        //     if (!val) { 
        //       //this.formList.find(x => x.fieldName == 'modelName').initialValue = '';
        //       this.formList.find(x => x.fieldName == 'optionName').initialValue = '';
        //     }
        //   }
        // },
        // {
        //   type: 'INPUT',
        //   label: '车型名称',
        //   fieldName: 'modelName',
        //   placeholder: '请输入',
        //   initialValue: '',
        //   disabled: true
        // },
        // {
        //   type: 'INPUT',
        //   label: '选装名称',
        //   fieldName: 'optionName',
        //   initialValue: '',
        //   placeholder: '请输入',
        //   disabled: true
        // },
        // {
        //   type: 'INPUT',
        //   label: '配置名称',
        //   fieldName: 'configName',
        //   initialValue: '',
        //   placeholder: '请输入',
        //   disabled: true
        // },
        // {
        //   type: 'INPUT',
        //   label: '车型代码',
        //   fieldName: 'modelCode',
        //   initialValue: '',
        //   placeholder: '请输入',
        //   disabled: true
        // },
           {
          type: 'INPUT',
          label: '物料编码',
          fieldName: 'productCode',
          initialValue: '',
          placeholder: '请输入',
          disabled: true,
           rules: [{ required: true,message: '请输入' }],
        },
        {
          type: 'INPUT',
          label: '物料描述',
          fieldName: 'productName',
          initialValue: '',
          placeholder: '请输入',
          disabled: true
        },
        {
          type: 'INPUT_NUMBER',
          label: '现金折让',
          fieldName: 'discountPrice',
          initialValue: '',
          placeholder: '请输入',
          rules: [{ required: true,message: '请输入' ,trigger: 'blur'}],
        },
     
        {
          type: 'DATE',
          label: '有效开始日期',
          fieldName: 'startDate',
          initialValue: moment().format('YYYY-MM-DD'),
          dateType: 'exactdate',
          minDateTime: moment().add(-1, 'days'),
          placeholder: '请输入',
          disabled :'true',
          rules: [{ required: true,message: '请选择时间' }],
          change: val => {
            if (val) {
              this.formList.find(x => x.fieldName == 'endDate').initialValue = '';
              this.formList.find(x => x.fieldName == 'endDate').minDateTime = moment(new Date(val)).add(-1, 'days');
            }
          }
          
        },
        {
          type: 'DATE',
          label: '有效结束日期',
          fieldName: 'endDate',
          minDateTime: moment().add(-1, 'days'),
          dateType: 'exactdate',
          disabled :'true',
        },
        //    {
        //   type: 'SELECT',
        //   label: '启用状态',
        //   fieldName: 'status',
        //   placeholder: '请选择',
        //   itemList: this.createDictList('1003'),
        //   rules: [{ required: true, message: '请选择', trigger: 'change' }]
        // },
        {
          type: 'INPUT',
          label: '品牌',
          fieldName: 'brandName',
          initialValue: '',
          placeholder: '请输入',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '车系',
          fieldName: 'seriesName',
          initialValue: '',
          placeholder: '请输入',
          disabled: true
        },

        {
          type: 'INPUT',
          label: '车型',
          fieldName: 'modelName',
          placeholder: '请输入',
          disabled: true
        },
         {
          type: 'INPUT',
          label: '配置',
          fieldName: 'packageName',
          placeholder: '请输入',
          disabled: true
        },
       {
          type: 'INPUT',
          label: '选装',
          fieldName: 'optionName',
          placeholder: '请输入',
          disabled: true
        },
       {
          type: 'INPUT',
          label: '内饰',
          fieldName: 'trimName',
          placeholder: '请输入',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '外饰',
          fieldName: 'colorName',
          placeholder: '请输入',
          disabled: true
        }
      ];
    }
  }
};
</script>
